/************************************************************************
VUE/ZoneDeTravail.java - Copyright ka791748

Here you can write a license for your code, some comments or any other
information you want to have in your generated code. To to this simply
configure the "headings" directory in uml to point to a directory
where you have your heading files.

or you can just replace the contents of this file with your own.
If you want to do this, this file is located at

/usr/share/apps/umbrello/headings/heading.java

-->Code Generators searches for heading files based on the file extension
i.e. it will look for a file name ending in ".h" to include in C++ header
files, and for a file name ending in ".java" to include in all generated
java code.
If you name the file "heading.<extension>", Code Generator will always
choose this file even if there are other files with the same extension in the
directory. If you name the file something else, it must be the only one with that
extension in the directory to guarantee that Code Generator will choose it.

you can use variables in your heading files which are replaced at generation
time. possible variables are : author, date, time, filename and filepath.
just write %variable_name%

This file was generated on jeu mar 31 2011 at 14:39:43
The original location of this file is /home/ka791748/INF5153/INF5153_TP2_PLAN_B/VUE/ZoneDeTravail.java
 **************************************************************************/
package INF5153_TP3.VUE;


import INF5153_TP3.Modele.*;
import INF5153_TP3.Controlleur.*;

import java.util.Observable;
import java.util.Observer;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class ZoneDeTravail extends JFrame implements Observer, ActionListener {

    // Déclaration de la zone de texte
    private JTextArea zoneTexte;
    private JTextField entreeTexte;
    private JLabel label_message;
    // Déclaration du curseur qui sera associé à la zone de texte
    private Caret curseur;



    // Déclaration des boutons
    JButton bouton_inserer;
    JButton bouton_copier;
    JButton bouton_coller;
    JButton bouton_couper;
    JButton bouton_refaire;
    JButton bouton_defaire;
    JButton bouton_rejouer;
    JButton bouton_selectionner;
   
    // le modele observé
    Buffer le_modele;

    public ZoneDeTravail(Buffer un_modele) {

         le_modele = un_modele;
         le_modele.addObserver(this);

         DefaultCaret le_caret = new DefaultCaret();

        le_caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        le_caret.setVisible(true);
        le_caret.setBlinkRate(1000);

        curseur = le_caret;

        // Affichage de la position du caret
        curseur.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                // La position dans le texte
                label_message.setText("> " + zoneTexte.getCaretPosition() + " <");

            }
        });
        

        zoneTexte = new JTextArea();
        zoneTexte.setEditable(false);
        zoneTexte.setCaret(curseur);
        zoneTexte.setCaretColor(Color.BLACK);
        zoneTexte.setBackground(Color.LIGHT_GRAY);


        // À enlever   OU BIEN à utiliser si on veux une version d'interface GRAPHIQUE
        // INUTILE dans la version en mode d'interface TEXTE
//        zoneTexte.addKeyListener(new KeyListener() {
//
//            public void keyTyped(KeyEvent e) {
//                throw new UnsupportedOperationException("Not supported yet.");
//            }
//
//            public void keyPressed(KeyEvent e) {
//                throw new UnsupportedOperationException("Not supported yet.");
//            }
//
//            public void keyReleased(KeyEvent e) {
//                throw new UnsupportedOperationException("Not supported yet.");
//            }
//        });

        zoneTexte.getCaret().setVisible(true);


        // ++++++++++++++++++++++++++++++++++++++++++++
        // À partir d'ici on construit le JFrame
        this.setTitle("INF5153 TP3");

        JPanel panel_TP3 = new JPanel(new BorderLayout());

        JScrollPane jsp = new JScrollPane(zoneTexte);
        zoneTexte.setText("");
        panel_TP3.add(jsp, BorderLayout.CENTER);

        // Ajout du panel du bas
        JPanel panel_du_bas = new JPanel(new BorderLayout());
        

        // Le textField pour l'entrée du texte
        entreeTexte = new JTextField(100);

        JPanel panel_entree_texte = new JPanel(new BorderLayout());
        
        panel_entree_texte.add(new JLabel(" Entrez votre texte ici --> "), BorderLayout.WEST);
        panel_entree_texte.add(entreeTexte, BorderLayout.CENTER);
        panel_entree_texte.add(new JLabel("      "), BorderLayout.EAST);
        panel_du_bas.add(panel_entree_texte, BorderLayout.NORTH);

        // Ajouter les boutons
        bouton_inserer = new JButton("Insérer");
        bouton_copier = new JButton("Copier");
        bouton_coller = new JButton("Coller");
        bouton_couper = new JButton("Couper");
        bouton_refaire = new JButton("Refaire");
        bouton_defaire = new JButton("Défaire");
        bouton_rejouer = new JButton("Rejouer");
        bouton_selectionner = new JButton("Sélectionner");

        // On ajoute les événements
        bouton_inserer.addActionListener(this);
        bouton_copier.addActionListener(this);
        bouton_coller.addActionListener(this);
        bouton_couper.addActionListener(this);
        bouton_refaire.addActionListener(this);
        bouton_defaire.addActionListener(this);
        bouton_rejouer.addActionListener(this);
        bouton_selectionner.addActionListener(this);

        JPanel panel_bouton = new JPanel(new FlowLayout());
        panel_bouton.add(bouton_inserer);
        panel_bouton.add(bouton_copier);
        panel_bouton.add(bouton_coller);
        panel_bouton.add(bouton_couper);
        panel_bouton.add(bouton_refaire);
        panel_bouton.add(bouton_defaire);
        panel_bouton.add(bouton_rejouer);
        panel_bouton.add(bouton_selectionner);
        panel_du_bas.add(panel_bouton, BorderLayout.CENTER);

        label_message = new JLabel("  ");
        label_message.setSize(600, 30);
        label_message.setBorder(BorderFactory.createLineBorder(Color.yellow));

        panel_du_bas.add(label_message, BorderLayout.SOUTH);


        panel_TP3.add(panel_du_bas, BorderLayout.SOUTH);



        // On place le tout
        this.add(panel_TP3);
        pack();
        this.setSize(700, 500);

        // Centrer le tout
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int w = getSize().width;
        int h = getSize().height;
        int x = (dim.width - w) / 2;
        int y = (dim.height - h) / 2;
        setLocation(x, y);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        entreeTexte.requestFocus();


    }


    //    gererCommandes() !
    public void actionPerformed(ActionEvent e) {

      Commande c=null;

      if (e.getSource() == bouton_inserer)
      {   c = new DecorateurCommande (new Inserer(zoneTexte.getCaretPosition(), entreeTexte.getText())); 
          c.setLe_modele(le_modele);
      }

      if (e.getSource() == bouton_coller)
      {   c = new DecorateurCommande (new Coller(zoneTexte.getCaretPosition()));
          c.setLe_modele(le_modele);
      }

      if (e.getSource() == bouton_copier)
      {   c = new DecorateurCommande (new Copier());
          c.setLe_modele(le_modele);
      }

      if (e.getSource() == bouton_couper)
      {   c = new DecorateurCommande (new Couper());
          c.setLe_modele(le_modele);
      }

      if (e.getSource() == bouton_refaire)
      {   c = new Refaire();
          c.setLe_modele(le_modele);
      }

      if (e.getSource() == bouton_defaire)
      {   c = new Defaire();
          c.setLe_modele(le_modele);
      }

      if (e.getSource() == bouton_rejouer)
      {   c = new RejouerCommandes();
          c.setLe_modele(le_modele);
      }

      if (e.getSource() == bouton_selectionner)
      {   c = new DecorateurCommande (
                   new Selectionner( zoneTexte.getSelectionStart(), zoneTexte.getSelectionEnd() ));
          c.setLe_modele(le_modele);
      }


      // Et voilà
      c.execute();


    }


    /**
     * @param        texte
     */
    public void update(Observable o, Object arg) {

        Message_Buffer recu = (Message_Buffer) arg;

        zoneTexte.setText(recu.getTexte());
        zoneTexte.getCaret().setVisible(true);
        entreeTexte.setText("");

        label_message.setText(recu.getMessage());
        entreeTexte.requestFocus();

    }

    // On relie les Observer et Observable
    public void setModel(Observable model) {
        this.le_modele = (Buffer) model;
        le_modele.addObserver(this);
        le_modele.notifyObservers();
    }



    // GO
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            Buffer un_modele = new Buffer();

            public void run() {
                new ZoneDeTravail(un_modele).setVisible(true);
            }
        });
    }

}
